﻿#include "inputitem.h"
#include "ui_inputitem.h"

InputItem::InputItem(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::InputItem)
{
    ui->setupUi(this);

    // 取消窗口最大化功能
    setWindowFlags(windowFlags() & ~Qt::WindowMaximizeButtonHint);

    // 固定窗体大小
    setFixedSize(this->width(),this->height());

    initComboBox();
}

InputItem::~InputItem()
{
    delete ui;
}

void InputItem::on_pushButton_clicked()
{
    QString strComId = ui->comboBox_id->currentText();
    QSqlQuery query;

    if(ui->lineEdit_num->text().isEmpty()) {
        QMessageBox::critical(this,"Warning",QStringLiteral("数量不能为空！"));
        return;
    }

    QString sql = QString("select * from commoditydatatable where Stockid=%1").arg(strComId);
    query.exec(sql);
    int i = 0;
    QString strNum;
    while (query.next()) {
        strNum = query.value(2).toString();
    }
    int inputNum = ui->lineEdit_num->text().toInt();
    int iSum = inputNum + strNum.toInt();

    QString res = QString::number(iSum);

    QString uSql = QString("update commoditydatatable set Stockamount=%1 where Stockid=%2").arg(res).arg(strComId);

    if(query.exec(uSql)) {
        QMessageBox::information(this,"Tip",QStringLiteral("入库成功！"));
    } else {
        QMessageBox::critical(this,"Warning",QStringLiteral("入库失败！！"));
    }
}

void InputItem::initComboBox()
{
    int i = 0;
    QSqlQuery query;
    query.exec("select * from commoditydatatable");

    QString id;
    while (query.next()) {
        id = query.value(0).toString();
        ui->comboBox_id->insertItem(i,id);
        i++;
    }
}

void InputItem::on_pushButton_out_clicked()
{
    QString strComId = ui->comboBox_id->currentText();
    QSqlQuery query;

    if(ui->lineEdit_num->text().isEmpty()) {
        QMessageBox::critical(this,"Warning",QStringLiteral("数量不能为空！"));
        return;
    }

    QString sql = QString("select * from commoditydatatable where Stockid=%1").arg(strComId);
    query.exec(sql);
    int i = 0;
    QString strNum;
    while (query.next()) {
        strNum = query.value(2).toString();
    }
    int inputNum = ui->lineEdit_num->text().toInt();
    int iRemain = strNum.toInt() - inputNum;

    QString res = QString::number(iRemain);

    QString uSql = QString("update commoditydatatable set Stockamount=%1 where Stockid=%2").arg(res).arg(strComId);

    if(query.exec(uSql)) {
        QMessageBox::information(this,"Tip",QStringLiteral("出库成功！"));
    } else {
        QMessageBox::critical(this,"Warning",QStringLiteral("出库失败！！"));
    }
}
